﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlogEngine.Core;
using AzureProvider.ssdsClient;
using System.Globalization;

namespace AzureProvider.SoupMethods
{
    public class ProfileMethods :BaseMethods 
    {
        protected override string containerId
        {
            get { return "Profiles"; }
        }

        protected override string kind
        {
            get { return "Profile"; }
        }

        protected override AzureProvider.ssdsClient.Entity CreateEntity(object Object)
        {
            AuthorProfile myobj = (AuthorProfile)Object;

            var entity = new Entity()
            {
                Id = myobj.Id.ToString(),
                Kind = kind
            };

            entity.Properties = new Dictionary<string, object>();
            entity.Properties.Add("DisplayName", (myobj.DisplayName==null)?"":myobj.DisplayName);
            entity.Properties.Add("FirstName", (myobj.FirstName == null) ? "" : myobj.FirstName);
            entity.Properties.Add("MiddleName", (myobj.MiddleName == null) ? "" : myobj.MiddleName);
            entity.Properties.Add("LastName", (myobj.LastName == null) ? "" : myobj.LastName);

            entity.Properties.Add("CityTown", (myobj.CityTown == null) ? "" : myobj.CityTown);
            entity.Properties.Add("RegionState", (myobj.RegionState == null) ? "" : myobj.RegionState);
            entity.Properties.Add("Country", (myobj.Country == null) ? "" : myobj.Country);
            
            entity.Properties.Add("Birthday",
                myobj.Birthday.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss",
                CultureInfo.InvariantCulture));
            entity.Properties.Add("AboutMe", (myobj.AboutMe == null) ? "" : myobj.AboutMe);
            entity.Properties.Add("PhotoURL", (myobj.PhotoURL == null) ? "" : myobj.PhotoURL);

            entity.Properties.Add("Company", (myobj.Company == null) ? "" : myobj.Company);
            entity.Properties.Add("EmailAddress", (myobj.EmailAddress == null) ? "" : myobj.EmailAddress);
            entity.Properties.Add("PhoneMain", (myobj.PhoneMain == null) ? "" : myobj.PhoneMain);
            entity.Properties.Add("PhoneMobile", (myobj.PhoneMobile == null) ? "" : myobj.PhoneMobile);
            entity.Properties.Add("PhoneFax", (myobj.PhoneFax == null) ? "" : myobj.PhoneFax);

            entity.Properties.Add("IsPrivate", myobj.IsPrivate);
            
            return entity;
           
        }

        public override object CreateObject(AzureProvider.ssdsClient.Entity Entity)
        {
            AuthorProfile myobj = new AuthorProfile
            {
                Id = Entity.Id,

                DisplayName = (string)Entity.Properties.Single(tt => tt.Key == "DisplayName").Value,
                FirstName = (string)Entity.Properties.Single(tt => tt.Key == "FirstName").Value,
                MiddleName = (string)Entity.Properties.Single(tt => tt.Key == "MiddleName").Value,
                LastName = (string)Entity.Properties.Single(tt => tt.Key == "LastName").Value,

                CityTown = (string)Entity.Properties.Single(tt => tt.Key == "CityTown").Value,
                RegionState = (string)Entity.Properties.Single(tt => tt.Key == "RegionState").Value,
                Country = (string)Entity.Properties.Single(tt => tt.Key == "Country").Value,

                Birthday = DateTime.Parse((string)Entity.Properties.Single(tt => tt.Key == "Birthday").Value),
                //DateModified = (DateTime)Entity.Properties.Single(tt => tt.Key == "DateModified").Value,
                AboutMe = (string)Entity.Properties.Single(tt => tt.Key == "AboutMe").Value,
                PhotoURL = (string)Entity.Properties.Single(tt => tt.Key == "PhotoURL").Value,

                Company = (string)Entity.Properties.Single(tt => tt.Key == "Company").Value,
                EmailAddress = (string)Entity.Properties.Single(tt => tt.Key == "EmailAddress").Value,
                PhoneMain = (string)Entity.Properties.Single(tt => tt.Key == "PhoneMain").Value,
                PhoneMobile = (string)Entity.Properties.Single(tt => tt.Key == "PhoneMobile").Value,
                PhoneFax = (string)Entity.Properties.Single(tt => tt.Key == "PhoneFax").Value,

                IsPrivate = (bool)Entity.Properties.Single(tt => tt.Key == "IsPrivate").Value,
               

            };

            return myobj;
        }
    }
}
